package com.talent.study.disruptor.example;

import com.lmax.disruptor.RingBuffer;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author: hszhou
 * @Date: 2025/4/15 9:48
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@Slf4j
public class OrderEventProducer {

    private final RingBuffer<OrderEvent> ringBuffer;

    public OrderEventProducer(RingBuffer<OrderEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    public void  sendData(String orderId){
        long sequence = ringBuffer.next();
        log.info("获取的序号是【{}】",sequence);
        try {
            OrderEvent event = ringBuffer.get(sequence);
            event.setOrderId(orderId);
        } finally {
            ringBuffer.publish(sequence);
            log.info("发送者发送数据【{}】",orderId);
        }
    }
}
